﻿using System.Collections.Generic;
using System.Linq;
using System;

namespace Citi.Helpers
{
    public static class MainArgument
    {
        public static string GetArgumentValue(string key, IEnumerable<string> args, char separator)
        {
            var arguments = args.ToList();

            if (arguments == null || !arguments.Any()) return string.Empty;

            var foundKey = arguments.FirstOrDefault(x => x.ToUpper() == string.Concat(separator, key.ToUpper()));

            if (foundKey == null || arguments.IndexOf(foundKey) + 1 >= arguments.Count()) return string.Empty;

            var nextArg = arguments[arguments.IndexOf(foundKey) + 1];

            if (string.IsNullOrWhiteSpace(nextArg) || nextArg.First() == separator)
                return string.Empty;

            return nextArg;
        }

        public static T GetArgumentValue<T>(string key, IEnumerable<string> args, char separator)
        {
            try
            {
                return (T)Convert.ChangeType(GetArgumentValue(key, args, separator), typeof(T));
            }
            catch
            {
                return default(T);
            }
        }
    }
}
